/**
 * 自动创建的对应js目录
 * 创建时间：2018-05-18 17:33:14
 * 技术支持：曹传喜
 * 邮箱：15075107120@139.com
 */
$(function () {
    initDatagrid();
    initToolbar();
});

function initToolbar() {
    $('#queryButton').click(function () {
        //通过datagrid的load方法执行查询并加载
        $('#listTable').datagrid('load', $('#queryForm').serializeObject());
    });
}

//初始化表格
function initDatagrid() {
    var url = $.util.getRequestJson('/json/rulescfg.warnList.json');
    var listTable = $('#listTable').datagrid({
        url: url,
        idField: 'id',
        fit: true,
        pagination: true,
        pageSize: 20,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        toolbar: '#toolbarButton',
        fitColumns: true,
        columns: [
            [
                {
                    field: 'ruleName',
                    title: '预警规则名称'
                },
                {
                    field: 'businessName',
                    title: '关联业务'
                },
                {
                    field: 'describe',
                    width: 1,
                    title: '预警描述'
                },
                {
                    field: 'status',
                    title: '状态',
                    align: 'center',
                    formatter: function (value, row) {
                        return value == 1 ? '启用' : '禁用';
                    }
                },
                {
                    field: 'id',
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row) {
                        var aEl = '<a href=javascript:edit("{0}"); class="easyui-tooltip iconImg icon-edit" title="编辑"></a>';
                        aEl += '&nbsp;&nbsp;<a href=javascript:updateStatus("{0}",-1); class="easyui-tooltip iconImg icon-remove" title="删除"></a>';
                        if (row.status == 1) {
                            aEl += '&nbsp;&nbsp;<a href=javascript:updateStatus("{0}",0); class="easyui-tooltip iconImg icon-disable" title="停用"></a>';
                        } else {
                            aEl += '&nbsp;&nbsp;<a href=javascript:updateStatus("{0}",1); class="easyui-tooltip iconImg icon-ok" title="启用"></a>';
                        }
                        //格式化操作按钮
                        return $.string.format(aEl, row.id);
                    }
                }
            ]
        ]
    });
}

function edit(id) {
    var title = id ? '编辑业务预警' : '添加业务预警';
    var url = $.util.getRootPath() + '/page/rulescfg/warn/edit.html';
    url = id ? url + '?id=' + id : url;
    var dialog = $.easyui.showDialog({
        title: title,
        width: 700,
        height: 450,
        maximizable: true,
        href: url,
        topMost: true,
        saveButtonText: '保存',
        onSave: function () {
            subForm(dialog);
        },
        buttons: [{
            text: '测试sql',
            handler: function () {
                validate();
            }
        }]
    });
}

function subForm(dialog) {
    var url = $.util.getRootPath() + '/rulescfg/saveWarn';
    var form = parent.$('#warnForm').form('submit', {
        url: url,
        onSubmit: function () {
            return parent.$('#warnForm').form('validate');
        },
        success: function (data) {
            if (data.success) {
                $('#listTable').datagrid('reload');
                dialog.dialog('close');
            }
            $.easyui.messager.show(data.message);
        }
    });
}

function validate() {
    var url = $.util.getRequestJson('/json/rulescfg.validateWarn.json');
    var param = {
        describe: parent.$('#describe').val(),
        sql: parent.$('#sql').val()
    };
    $.util.post(url, param, function (json) {
        if (json.success) {
            $.easyui.messager.show("测试通过！");
        } else {
            $.easyui.messager.show(json.message);
        }
    });
}

function updateStatus(id, status) {
    var msg = '确定要删除吗？';
    if (status == 1) {
        msg = '确定要启用吗？';
    } else if (status == 0) {
        msg = '确定要禁用吗？';
    }
    $.easyui.messager.confirm(msg, function (r) {
        if (r) {
            var url = $.util.getRequestJson('/json/rulescfg.updateWarnStatus.json');
            var param = {id: id, status: status, type: 1, row: 10};
            $.util.post(url, param, function (json) {
                if (json.success) {
                    $('#listTable').datagrid('reload');
                }
                $.easyui.messager.show(json.message);
            });
        }
    });
}

